//
//  AFNetRequest.m
//  YLNetworkingFrameworks
//
//  Created by Topsun on 16/6/1.
//  Copyright © 2016年 tcl. All rights reserved.
//

#import "AFNetRequest.h"

@implementation AFNetRequest

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 默认是POST请求
        _requestMethod = @"POST";
        
        // 默认网络优先
        self.requestPriority = RequestPriorityNetWork;
    }
    return self;
}

// 获当前请求的唯一标示（请求地址加上请求参数）
- (NSString*) getRequestUUID
{
    return @"topsun";
}

-(ParameterEntity *)parameterEntity
{
    if (_parameterEntity == nil) {
        _parameterEntity = [[ParameterEntity alloc]init];
    }
    return _parameterEntity;
}


-(NSURL *)requestURL
{
    NSString * requestURL = @"";
    
    if (self.requestTypeKey != nil)
    {
        // 获取API接口服务器
        NSString *apiURL = [SC getServiceHostURLString];
        // 根据接口类型，获取API接口地址
        NSString *url = [SC getAPIURLForInterfacekey:self.requestTypeKey];
        // 创建API接口
        requestURL = [NSString stringWithFormat:@"%@%@", apiURL, url];
    }
    return [NSURL URLWithString:requestURL];
}

-(NSString *)requestMethod
{
    // 从配置器中获取当前接口请求方式，默认是POST
    NSString * method = [SC getRequestMethodForInterfacekey:self.requestTypeKey];
    
    if ( method == nil || method.length == 0 )
    {
        // 如果配置器中无该配置，返回默认值
        return _requestMethod;
    }
    return method;
}

@end


@implementation ParameterEntity

@synthesize headDictionary = _headDictionary;
@synthesize postDictionary = _postDictionary;
@synthesize getDictionary = _getDictionary;

// 添加一个Post请求参数
- (void) setPostValue:(id<NSObject>)value forKey:(NSString *)key
{
    [self setValue:value forKey:key forMethod:@"POST"];
}

// 添加一个Get请求参数
- (void) setGetValue:(id<NSObject>)value forKey:(NSString *)key
{
    [self setValue:value forKey:key forMethod:@"GET"];
}

// 添加一个Head请求参数
- (void) setHeadValue:(id<NSObject>)value forKey:(NSString *)key
{
    [self setValue:value forKey:key forMethod:@"HEAD"];
}

// 添加一个参数
- (void) setValue:(id<NSObject>)value forKey:(NSString *)key forMethod:(NSString*) method
{
    NSMutableDictionary *tempTable = [NSMutableDictionary dictionary];
    
    if ( [method isEqualToString:@"POST"] ) {
        tempTable = self.postDictionary;
    } else if ( [method isEqualToString:@"GET"] ) {
        tempTable = self.getDictionary;
    } else if ( [method isEqualToString:@"HEAD"] ) {
        tempTable = self.headDictionary;
    }
    
    if ( key != nil) {
        if (value != nil)
        {
            [tempTable setObject:value forKey:key];
        }else{
            [tempTable removeObjectForKey:key];
        }
    }
}

- (NSMutableDictionary*) headDictionary
{
    if ( nil == _headDictionary ) {
        _headDictionary = [[NSMutableDictionary alloc] init];
    }
    
    return _headDictionary;
}

- (NSMutableDictionary*) getDictionary
{
    if ( nil == _getDictionary ) {
        _getDictionary = [[NSMutableDictionary alloc] init];
    }
    
    return _getDictionary;
}

- (NSMutableDictionary*) postDictionary
{
    if ( nil == _postDictionary ) {
        _postDictionary = [[NSMutableDictionary alloc] init];
    }
    
    return _postDictionary;
}

@end